package com.mf.all.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.emuns.enumsPublic.EnumPublicDefault;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.mf.all.entity.dto.DtoCustomerAddr;
import com.mf.all.entity.req.search.customer.SearchReqCustomerAddr;
import com.mf.all.mapper.MapperCustomerAddr;
import com.mf.all.service.ServiceCustomerAddr;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ImplServiceCustomerAddr extends ServiceImpl<MapperCustomerAddr, DtoCustomerAddr> implements ServiceCustomerAddr {
    /**
     * 设置默认客户地址
     */
    @Override
    @Transactional
    public void updateDefaultAddr(Integer customerId, Integer id) {
        // 把所有的地址设置为非默认地址
        final LambdaUpdateWrapper<DtoCustomerAddr> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(DtoCustomerAddr::getIsDefault, EnumPublicDefault.NO_DEFAULT.getCode())
                .eq(DtoCustomerAddr::getCustomerId, customerId);
        update(wrapper);
        // 当前地址设置为默认地址
        final DtoCustomerAddr dtoCustomerAddr = new DtoCustomerAddr();
        dtoCustomerAddr.setIsDefault(EnumPublicDefault.DEFAULT.getCode());
        dtoCustomerAddr.setId(id);
        dtoCustomerAddr.setCustomerId(customerId);
        updateById(dtoCustomerAddr);
    }

    @Override
    public LambdaQueryWrapper<DtoCustomerAddr> fillParams(SearchReqCustomerAddr params) {
        final LambdaQueryWrapper<DtoCustomerAddr> wrapper = loadQw();
        if (StringUtils.isNotBlank(params.getKey())) {
            wrapper.and(w -> {
                w.like(DtoCustomerAddr::getAddr, params.getKey());
            });
        }

        wrapper.eq(DtoCustomerAddr::getCustomerId, params.getCustomerId())
                .eq(ObjectUtils.isNotEmpty(params.getStatus()), DtoCustomerAddr::getStatus, params.getStatus())
                .eq(ObjectUtils.isNotEmpty(params.getIsDefault()), DtoCustomerAddr::getIsDefault, params.getIsDefault())
        ;
        return wrapper;
    }

}